<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html >
<html lang="en">
<head>
<%@include file="../../common/includeBaseSet.jsp" %>
<%@include file="../../common/includeSystemSet.jsp" %>
<%@include file="../../common/operation_forbid.jsp" %>
<%@include file="../../common/jsVersion.jsp" %>
<style type="text/css">
	a{cursor:pointer}
	td{padding-top: 5px;}
	.item-info-surround{border:1px solid #dcdcdc;margin-bottom: 10px;}
	.item-title{line-height: 30px;height: 30px;border-bottom: 1px solid #dcdcdc;color: #444;font-size: 14px;background-color: #efefef;padding: 0 10px;font-weight: bold;}
	.thisBtn{width: 80px;height: 28px;padding: 0;background-color: #009999; border:1px solid #FFFFFF;color: #FFFFFF;margin-right: 20px;
			 line-height: 28px;border-radius: 4px;}
	.thisBtn:hover,.thisBtn:focus{background-color: #f87242;border:1px solid #f87242;}
    .thisCleanBtn{width: 80px;height: 28px;padding: 0;background-color: #E4E4E4; border:1px solid #0f0f0f; color: #0f0f0f;margin-right: 20px;
        line-height: 28px;border-radius: 4px;}
    .thisCleanBtn:hover,.thisCleanBtn:focus{background-color: #f87242;border:1px solid #f87242;}
    .thisSmallBtn{width: 40px;height: 28px;padding: 0;background-color: #fff;border:1px solid #f87242;color: #f87242;margin-right: 20px;
        line-height: 28px;border-radius: 4px;}
    .thisSmallBtn:hover,.thisSmallBtn:focus{background-color: #f5f5f5;border:1px solid #d8d8d8;}
</style>
</head>
<body>
    <div class="page-content">
        <form id="baseForm" class="form-horizontal">
            <input type='hidden' class='pageNum' name='pageNum' value='1'/>
            <input type='hidden' class='pageSize' name='pageSize' value='10'/>
	        <div class="item-info-surround">
		        <table style="width:100%">
		        	<tr>
		        		<td colspan="2" class="item-title">项目参数设置</td>
		        	<tr>
		        	<tr>
		        		<td align="right" width="120px" style="padding-top: 20px">项目名称：</td>
		        		<td style="padding-top: 20px">
		        			<select class="form-control" id="projectId" name="projectId" onchange="loadIndustMark()" style="width:200px">
	                        	<c:forEach items="${projectList}" var="list">
								    <option value="${list.id}">${list.projectName}</option>
							    </c:forEach>
	                        </select>
		        		</td>
		        	</tr>
		        	<tr>
<!-- 		        		<td align="right"><font style="color:red"><font></td> -->
		        		<td colspan="2" style="padding-left: 17px"><font style="color:red">提示：设置项目应用场景周期数据时，只可填写整数，单位以“天”计算！<font></td>
		        	</tr>
		        </table>
		        <table style="width:100%;margin-bottom: 10px" border="0" id="IndustMarkInput">
					<!-- 项目业态参数 -->		        	
		        </table>
	        </div>
	    </form>
        <div class="col-xs-12">
            <table id="baseTable" class="table table-striped table-bordered table-hover">
                <thead>
                    <tr>
                        <th class="center">编号</th>
                        <th class="center">应用场景</th>
                        <th class="center">周期类型</th>
                        <th class="center">周期数值</th>
                        <th class="center">设置人员</th>
                        <th class="center">设置日期</th>
                        <th class="center">是否有效</th>
                    </tr>
                </thead>
                <tbody><!-- 内容 --></tbody>
            </table>
            <div class="row">
                <div class="col-sm-8">
                    <!--设置分页位置-->
                    <div id="pageing" class="dataTables_paginate paging_bootstrap">
                        <ul class="pagination"></ul>
                    </div>
                </div>
            </div>
        </div>
        <!-- #dialog-confirm -->
        <%@include file="../../common/dialog.jsp"%>
    </div>
</body>
<script type="text/javascript">
	$(function(){
		loadIndustMark();
	});
	//业态数组
	var array = [];
	//设置周期类型
	var fields = ['conversionSign', 'receivable'];
	
	//获取所选项目业态
	function loadIndustMark(){
		//清空业态数组及业态输入框html
		array = [];
		$("#IndustMarkInput").empty();
		var projectId = $("#projectId").val();
		$.ajax({
			type: 'post',
			url: '${jypath}/estate/projectParameter/getIndustMarkByProjectId',
			data: {
				"projectId": projectId
			},
			dataType: 'json',
			success: function(data){
				if(data.res == "0"){
					layer.msg(data.resMsg);
					return false;
				}else if(data.obj.entity == null){
					layer.msg("未获取到该项目的应用场景");
					return false;
				}
				setIndustMarkInput(data.obj.entity);
			}
		});
		//获取该项目下的历史参数
		JY.Page.jump('baseForm',1,'loadProjectParameterPage')
	}
	
	//生成业态输入框
	function setIndustMarkInput(entity){
        var projectId = $("#projectId").val();
		//1、循环生成业态输入框html
        var html = "";
		for(var i = 0; i < entity.length; i++){
			var typeId = entity[i].type; //业态ID
			var typeName = entity[i].typeName; //业态名称
			array.push(typeId); //将业态ID保存到数组
			//生成业态输入框
			html += '<tr>';
			//循环生成周期类型
			for(let cycleType of fields){
				var name = "认购转签约";
				var value = "7";
				if(cycleType == "receivable"){
					name = "收款";
					value = "30";
				}
				html+= '<td align="left" width="200px">'+name+'周期（'+typeName+'）：</td>'
                    + '<td>'
                    + '<input class="form-control" style="width:95%" type="number" min="1" name="'+cycleType+'_'+typeId+'" id="'+cycleType+'_'+typeId+'" value="'+value+'" oninput="if(value.length>3)value=value.slice(0,3)"/>'
                    + '</td>'
                    + '<td>'
                    + '<input type="button" id="saveSingle" class="thisSmallBtn" onclick="saveProjectSingleParameter(\''+cycleType+'\', '+typeId+')" value="保存"/>'
                    + '</td>';
			}
    		html += '</tr>';
		}
        <!-- 添加弃客标签 -->
        cycleType = "giveUp";
        html += '<tr>';
        html+= '<td align="left" width="200px">客户放弃周期：</td>'
            + '<td>'
            + '<input class="form-control" style="width:95%" type="number" min="1" name="giveUp_'+projectId+'" id="giveUp_'+projectId+'" value="45"/>'
            + '</td>'
            + '<td>'
            + '<input type="button" id="saveSingle" class="thisSmallBtn" onclick="saveProjectSingleParameter(\''+cycleType+'\', '+projectId+')" value="保存"/>'
            + '</td>';
        html += '</tr>';
        $("#IndustMarkInput").append(html);
		//2、获取项目参数值
		getProjectParameter();
		//3、生成保存按钮
		var htmlBtn = '<tr>'
		      		+ '<td align="right" colspan="6" style="padding-top: 20px;padding-bottom: 10px; padding-right: 88px;">'
		      		+ '<input type="button" id="save" class="thisBtn" onclick="saveProjectParameter()" value="确定"/>'
		      		+ '<input type="button" id="clean" class="thisCleanBtn" value="清空" onclick="cleanInput()"/>'
		      		+ '</td>'
		      		+ '</tr>';
		$("#IndustMarkInput").append(htmlBtn);
	}
	
	//获取所选项目的参数值
	function getProjectParameter(){
		var projectId = $("#projectId").val();
		$.ajax({
			type: 'post',
			url: '${jypath}/estate/projectParameter/list',
			data: {
				'projectId': projectId
			},
			dataType: 'json',
			success: function(data){
				if(data.res == "0"){
					layer.msg(data.resMsg);
					return false;
				}
				var entity = data.obj.entity;
				for(var i = 0; i < entity.length; i++){
					var info = entity[i];
					var key = "#" + info.cycleType + "_" + info.type;
					$(key).val(info.cycleValue);

				}
			}
		});
	}
	
	//保存项目参数
	var flag = true;
	function saveProjectParameter(){
        var projectId = $("#projectId").val();
		//判断是否已提交
		if(!flag){
			layer.msg("保存中，请勿重复提交");
			return false;
		}
		flag = false;
		if(array.length == 0){
			layer.msg("保存失败，请刷新页面后重试");
			return false;
		}
		var isNotNumber = false;
		//初始化保存项目参数json的数据
		var arr = new Array();
		//遍历业态数组
		for(let type of array){
			//遍历周期类型
			for(let cycleType of fields){
				var cycleValue = $("#"+cycleType+"_"+type).val();
				console.log(!(/(^[1-9]\d*$)/.test(cycleValue)));
				//判断数字正确性
				if(cycleValue == "" || isNaN(cycleValue) || !(/(^[1-9]\d*$)/.test(cycleValue))){
					isNotNumber = true;
				}
				var json = {
			    	"type": type,
			    	"cycleType": cycleType,
			    	"cycleValue": cycleValue
				};
				arr.push(json);
			}
		}
        var giveUpValue = $("#giveUp_" + projectId).val();
        //判断数字正确性
        if(giveUpValue == "" || isNaN(giveUpValue) || !(/(^[1-9]\d*$)/.test(giveUpValue))){
            isNotNumber = true;
        }
        var json = {
            "type": projectId,
            "cycleType": "giveUp",
            "cycleValue": giveUpValue
        };
        arr.push(json);

		if(isNotNumber){
			layer.msg("请填写正确的周期数");
			flag = true;
			return false;
		}
		
		$.ajax({
			type: 'post',
			url: '${jypath}/estate/projectParameter/add',
			data: {
				'json': JSON.stringify(arr),
				'projectId': $("#projectId").val()
			},
			dataType: 'json',
			success: function(data){
				layer.msg(data.resMsg);
				if(data.res == "1"){
					//保存成功时，刷新列表
					loadProjectParameterPage();
					flag = true;
				}
			}
		});
	}
	
	//项目参数设置分页查询
	function loadProjectParameterPage(){
		JY.Ajax.doRequest("baseForm",jypath +'/estate/projectParameter/getPage',null,function(data){
            $("#baseTable tbody").empty();
            var page = data.obj;
            //分页数据
            var pageNum = page.pageNumber, pageSize = page.pageSize, totalRecord = page.total;
            var html = "";
            var results = page.rows;
            if(results == null || results.length <= 0){
            	html+="<tr><td colspan='7' class='center'>没有相关数据</td></tr>";
                $("#baseTable tbody").append(html);
                $("#pageing ul").empty();//清空分页
                return false;
            }
            var leng=(pageNum-1)*pageSize;//计算序号
            for(var i = 0;i<results.length;i++){
            	var info = results[i];
            	var cycleName = "认购转签约";
            	if(info.cycleType == "receivable"){
            		cycleName = "收款";
            	}else if(info.cycleType == "giveUp"){
            	    info.typeName = "项目级";
            	    cycleName = "客户放弃";
            	}
            	var delFlag = "无效";
            	if(info.delFlag == "0"){
            		delFlag = "有效";
            	}
            	html += '<tr>'
            		  + '<td class="center">' + (leng + i + 1) + '</td>'
            		  + '<td class="center">' + info.typeName + '</td>'
            		  + '<td class="center">' + cycleName + '</td>'
            		  + '<td class="center">' + info.cycleValue + '天</td>'
            		  + '<td class="center">' + info.userName + '</td>'
            		  + '<td class="center">' + JY.Date.Format(info.createTime, 'yyyy-MM-dd')  + '</td>'
            		  + '<td class="center">' + delFlag + '</td>'
            		  + '</tr>'
            }
            $("#baseTable tbody").append(html);
            JY.Page.setPage("baseForm","pageing",pageSize,pageNum,totalRecord,"loadProjectParameterPage");
		});
	}
	
	//清空数据
	function cleanInput(){
		//遍历业态数组
		for(let type of array){
			//遍历周期类型
			for(let cycleType of fields){
				$("#"+cycleType+"_"+type).val("");
			}
		}
	}
	
	//是否为正整数
	function isPositiveInteger(s){
	     var re = /^[0-9]+$/ ;
	     return re.test(s)
	}

	function saveProjectSingleParameter(cycleType, type){
        var isNotNumber = false;
        var projectId = $("#projectId").val();
        var cycleValue = $("#"+cycleType+"_"+type).val();
        console.log("projectId = " + projectId + "cycleType = " + cycleType + ", cycleValue = " + cycleValue + ", type = " + type);
        //判断数字正确性
        if(cycleValue == "" || isNaN(cycleValue) || !(/(^[1-9]\d*$)/.test(cycleValue))){
            isNotNumber = true;
        }
        var arr = new Array();
        var json = {
            "type": type,
            "cycleType": cycleType,
            "cycleValue": cycleValue
        };
        arr.push(json);
        if(isNotNumber){
            layer.msg("请填写正确的周期数");
            flag = true;
            return false;
        }

        $.ajax({
            type: 'post',
            url: '${jypath}/estate/projectParameter/addSingleParam',
            data: {
                'json': JSON.stringify(arr),
                'projectId': $("#projectId").val()
            },
            dataType: 'json',
            success: function(data){
                layer.msg(data.resMsg);
                if(data.res == "1"){
                    //保存成功时，刷新列表
                    loadProjectParameterPage();
                    flag = true;
                }
            }
        });
    }
</script>
</html>